package com.symone.webcms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.symone.webcms.mapper.MenuMapper;
import com.symone.webcms.model.Menu;
import com.symone.webcms.model.MenuAction;
import com.symone.webcms.model.vo.MenuVO;
import com.symone.webcms.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *　　
 *   @description : Menu 服务实现类
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-02
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public boolean insert(MenuVO vo) {
        Menu menu = vo.getMenu();
        if(baseMapper.insert(menu) > 0 ) {
            baseMapper.cleanAction(vo.getCode());
            if(vo.getActions() == null || updateAction(vo,menu.getCode())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(MenuVO dto) {
        Menu menu = dto.getMenu();
        if(baseMapper.updateById(menu) > 0 ) {
            baseMapper.cleanAction(dto.getCode());
            if(dto.getActions() == null || updateAction(dto,dto.getCode())){
                return true;
            }
        }
        return false;
    }

    @Override
    public Page<Menu> findPage(Page<Menu> page, Wrapper<Menu> menu) {
        List<Menu> result = baseMapper.selectPage(page,menu);
        page.setRecords(result);
        return page;
    }

    @Override
    public List<Menu> findAll() {
        Wrapper<Menu> ew = new EntityWrapper<Menu>();
        ew.where("deleted=0");
        return baseMapper.selectList(ew);
    }

    @Override
    public int delete(long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public boolean deleteFlagById(long id) {
        return baseMapper.deleteFlagById(id);
    }

    @Override
    public int clear() {
        return baseMapper.delete(new EntityWrapper<Menu>().eq("deleted" ,1));
    }


    private boolean updateAction(MenuVO menu,String mcode){
        String[] actions = menu.getActions();
        for(int i = 0 ; i < actions.length; i ++ ){
            MenuAction action = new MenuAction();
            action.setMcode(mcode);
            action.setCode(actions[i]);
            action.setShiroCode("web:"+mcode+":"+actions[i]);
            String mmcode = mcode.replace(":","/");
            switch (actions[i]){
                case "add":
                    action.setName("添加");
                    action.setUrl( "/web/" + mmcode +"/add");
                    action.insert();
                    continue;
                case "edit":
                    action.setName("编辑");
                    action.setUrl( "/web/" + mmcode +"/edit");
                    action.insert();
                    continue;
                case "remove":
                    action.setName("删除");
                    action.setUrl( "/api/" + mmcode +"/remove");
                    action.insert();
                    continue;
                default:
                    action.setName(actions[i]);
            }
        }
        return true;
    }


}
